package com.byl.modules.sys.controller;

import com.byl.common.modules.controller.BaseController;
import com.byl.common.modules.service.TokenService;
import com.byl.modules.sys.service.LoginService;
import com.byl.sdk.captcha.utils.CaptchaUtil;
import com.byl.sdk.utils.MD5Util;
import com.byl.sdk.utils.MapUtils;
import com.byl.sdk.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2019/7/29.
 */
@RestController

public class LoginController extends BaseController {

    @Autowired
    LoginService loginService;

    @Autowired
    TokenService tokenService;

    @PostMapping("/api/sys/login")
    public String login(@RequestBody Map map, HttpServletRequest request) {
        String cap=MapUtils.getValue(map,"capcha");
        if (StringUtil.isBlank(cap))
            return fail("验证码不能为空！");
        if (!CaptchaUtil.ver(cap,request)) {
            CaptchaUtil.clear(request);
            return fail("验证码不正确");
        }
        //判断用户名
        String username= MapUtils.getValue(map,"username");
        if (StringUtil.isBlank(username))
            return fail("用户名不能为空！");
        //获取密码
        String pwd=MapUtils.getValue(map,"password");
        if (StringUtil.isBlank(pwd))
            return fail("密码不能为空！");

        //根据用户名查询用户信息
        Map rs = loginService.queryUser(username);
        if (null == rs)
            return fail("该账号不存在！");

        String enpwd=MD5Util.getMD5(pwd);
        String dbpwd=MapUtils.getValue(rs,"password");
        if (!enpwd.equals(dbpwd))
            return fail("密码不正确！");

        int status = Integer.valueOf(rs.get("status").toString());
        if (status !=1 )
            return fail("该账号没有权限！");


        //去掉密码
        rs.remove("password");
        String token=tokenService.getToken(rs);
        rs.put("token",token);
        return success("登录成功", rs);
    }

    @PostMapping("/refreshToken")
    public String refreshToken(Map map){
        System.out.println("-----------------cros-------------");
        if(map.containsKey("token")){
            Object obj=map.get("token");
            if(null!=obj){
                String rs=tokenService.refreshToken(obj.toString());
                return success("token刷新",rs);
            }
        }
        return fail("no token");
    }

    /**
     * 合伙人或收单员在移动端登录
     * @param map
     * @return
     */
    @PostMapping("/wx/login")
    public String wxlogin(@RequestBody Map map){
        //获取用户名 根据用户名查询用户信息
        String username= MapUtils.getValue(map,"username");
        if (StringUtil.isBlank(username))
            return fail("用户名不能为空！");
        //获取密码
        String pwd=MapUtils.getValue(map,"password");
        if (StringUtil.isBlank(pwd))
            return fail("密码不能为空！");
                    //根据用户名查询合伙人类型
        Map rs= loginService.queryPartnerUser(map);
        if (null==rs){
            return fail("账号不存在！");
        }
        //判断密码是否正确
        //取出数据库中存的密码
        String tmp=MapUtils.getValue(rs,"password");
        if (!MD5Util.getMD5(pwd).equals(tmp))
            return  fail("密码不正确！");

        //获取用户状态
        int status=MapUtils.getIntValue(rs,"status");
        if (status!=1)
            return fail("该账号没有权限");
        //移除密码
        rs.remove("password");
        return success("登录成功！",rs);
    }
}
